// components/camp-goods/camp-goods.js
import {
    baseURL
  } from "../../utils/request"
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    campGoodsStatus: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    baseURL:baseURL,
    campGoodsStatus: false,
    addList: [], //整体数据
    campId: "",
    goodsId: "", //景点id
    depict: "",
    goodsImg: [],
    goodsName: '',
    memberPrice: '',
    goodsPrice: '',
  },
  lifetimes: {
    ready() {

    }
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    // 绑定营地介绍
    changeDepict(e) {
      this.setData({
        depict: e.detail.value
      })
    },
    //重置数据
    resetData() {
      this.setData({
        campId: "",
        goodsName: '',
        memberPrice: '',
        goodsPrice: '',
        depict: "",
        goodsImg: [],
      })
    },
    //添加虚拟数据
    addData(e) {

        var goodsImg=""
        for(var i in this.data.goodsImg){
            console.log(this.data.goodsImg[i])
            goodsImg+=this.data.goodsImg[i]+","
        }
        goodsImg=goodsImg.substr(0,goodsImg.length-1)
        console.log("goodsImg=",goodsImg)


      const object = {
        goodsName: e.detail.value.goodsName,
        memberPrice: Number(e.detail.value?.memberPrice),
        goodsPrice: Number(e.detail.value.goodsPrice),
        depict: this.data.depict,
        goodsImg:goodsImg
      }
      console.log(object);
      var isEmpty = true;
      Object.keys(object).forEach(function (x) {
        if (object[x] === null || object[x] === "") {
          isEmpty = false;
        }
      });
      if (isEmpty) {
        this.data.addList.push({
            goodsName: e.detail.value.goodsName,
            memberPrice: Number(e.detail.value?.memberPrice),
            goodsPrice: Number(e.detail.value.goodsPrice),
            depict: this.data.depict,
            goodsImg:goodsImg
        })
        this.resetData();
        this.onClose();
        this.triggerEvent("sendData", this.data.addList)
      } else {
        wx.showToast({
          title: "信息未填写完整",
          icon: "error",
          duration: 1500
        })
      }

      // console.log(instance.globalData.campData)
      console.log(this.data.addList);
    },
    onClose() {
      this.resetData()
      this.setData({
        campGoodsStatus: false
      })
    },
    //获取图片
  getUploadList(e) {
    console.log(e)
    this.setData({
        goodsImg: e.detail,
    //   tourisCampImgs: e.detail
    })
  },
   //删除图片
   delImgs(u) {
    this.data.goodsImg.splice(u.currentTarget.dataset.index, 1)
    this.setData({
        goodsImg: this.data.goodsImg
    })
  },
  }
})